package com.zzedu.integration.sms.impl;

import com.alibaba.fastjson2.JSON;
import com.google.common.base.Joiner;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import com.zzedu.integration.sms.ISmsClientService;
import com.zzedu.integration.sms.config.SmsConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 短信发送服务
 */
@Component
public class SmsClientServiceImpl implements ISmsClientService {
    private static final Logger log = LoggerFactory.getLogger(SmsClientServiceImpl.class);

    @Autowired
    private SmsConfig smsConfig;

    @Autowired
    private SmsClient smsClient;

    @Override
    public SendSmsResponse sendMsg(String[] phoneNumberSet, String[] templateParamSet, String templateId) {
        SendSmsRequest req = new SendSmsRequest();
        req.setSmsSdkAppId(smsConfig.getAppId());
        req.setSignName(smsConfig.getSignName());
        req.setTemplateId(templateId);
        req.setTemplateParamSet(templateParamSet);
        req.setPhoneNumberSet(phoneNumberSet);
        SendSmsResponse res = null;
        try {
            log.info("{},发送短信请求:{}", Joiner.on(",").join(phoneNumberSet), JSON.toJSONString(req));
            res = smsClient.SendSms(req);
            log.info("{},发送短信返回:{}", Joiner.on(",").join(phoneNumberSet), JSON.toJSONString(res));
        } catch (Exception e) {
            log.error("发送短信失败,详细:{}", e.getMessage());
            throw new RuntimeException("发送短信失败");
        }
        return res;
    }
}
